import cv2
import numpy as np

# 读取图像
image1 = cv2.imread('image1.png', cv2.IMREAD_GRAYSCALE)
image2 = cv2.imread('image2.png', cv2.IMREAD_GRAYSCALE)

# 确保图像尺寸相同
if image1.shape != image2.shape:
    raise ValueError("图像尺寸不一致")

# 计算像素差异
diff = np.sum(image1 != image2)
total_pixels = image1.size

# 计算像素差异率
pixel_diff_ratio = diff / total_pixels
print(f"Pixel Difference Ratio: {pixel_diff_ratio}")

#pip install scikit-image
from skimage.metrics import structural_similarity as ssim
import cv2

# 读取图像
image1 = cv2.imread('image1.png', cv2.IMREAD_GRAYSCALE)
image2 = cv2.imread('image2.png', cv2.IMREAD_GRAYSCALE)

# 计算 SSIM
ssim_value = ssim(image1, image2)
print(f"SSIM: {ssim_value}")



import cv2
import numpy as np
from skimage.metrics import structural_similarity as ssim

def calculate_pixel_diff_ratio(image1, image2):
    """计算像素差异率"""
    if image1.shape != image2.shape:
        raise ValueError("图像尺寸不一致")
    diff = np.sum(image1 != image2)
    total_pixels = image1.size
    return diff / total_pixels

def calculate_ssim(image1, image2):
    """计算 SSIM"""
    return ssim(image1, image2)

def compare_images(image1_path, image2_path, ssim_threshold=0.95, pixel_diff_threshold=0.05):
    """比较两幅图像，并判断是否通过测试"""
    # 读取图像
    image1 = cv2.imread(image1_path, cv2.IMREAD_GRAYSCALE)
    image2 = cv2.imread(image2_path, cv2.IMREAD_GRAYSCALE)

    # 计算 SSIM 和像素差异率
    ssim_value = calculate_ssim(image1, image2)
    pixel_diff_ratio = calculate_pixel_diff_ratio(image1, image2)

    # 输出结果
    print(f"SSIM: {ssim_value:.4f}")
    print(f"Pixel Difference Ratio: {pixel_diff_ratio * 100:.2f}%")

    # 判断是否通过测试
    if ssim_value >= ssim_threshold and pixel_diff_ratio <= pixel_diff_threshold:
        print("测试通过：图像相似度符合要求。")
        return True
    else:
        print("测试失败：图像相似度不符合要求。")
        return False

# 示例调用
image1_path = 'image1.png'
image2_path = 'image2.png'
ssim_threshold = 0.95  # SSIM 阈值
pixel_diff_threshold = 0.05  # 像素差异率阈值

result = compare_images(image1_path, image2_path, ssim_threshold, pixel_diff_threshold)
if result:
    print("测试成功！")
else:
    print("测试失败！")

#如果计算完全色彩的ssim：
def calculate_ssim_color(image1, image2):
    """计算彩色图像的 SSIM（分别计算每个通道，然后取平均值）"""
    ssim_values = []
    for i in range(3):  # 遍历三个通道（BGR）
        ssim_value = ssim(image1[:, :, i], image2[:, :, i])
        ssim_values.append(ssim_value)
    return np.mean(ssim_values)

def calculate_pixel_diff_ratio_color(image1, image2):
    """计算彩色图像的像素差异率（分别计算每个通道，然后取平均值）"""
    diff = np.sum(image1 != image2, axis=(0, 1))  # 按通道计算差异
    total_pixels = image1.size / 3  # 每个通道的像素数量
    return np.mean(diff / total_pixels)

import cv2
import numpy as np
from skimage.metrics import structural_similarity as ssim

def calculate_ssim_color(image1, image2):
    """计算彩色图像的 SSIM（分别计算每个通道，然后取平均值）"""
    if image1.shape != image2.shape:
        raise ValueError("图像尺寸不一致")

    # 初始化 SSIM 值列表
    ssim_values = []

    # 分别计算每个通道的 SSIM
    for i in range(3):  # 遍历三个通道（BGR）
        channel1 = image1[:, :, i]
        channel2 = image2[:, :, i]
        ssim_value = ssim(channel1, channel2)
        ssim_values.append(ssim_value)

    # 返回平均值
    return np.mean(ssim_values)

# 读取彩色图像
image1 = cv2.imread('image1.png', cv2.IMREAD_COLOR)
image2 = cv2.imread('image2.png', cv2.IMREAD_COLOR)

# 计算彩色图像的 SSIM
ssim_value = calculate_ssim_color(image1, image2)
print(f"彩色图像的 SSIM: {ssim_value:.4f}")